RIFFFile.getChunkId_   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2017-2019 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview The RIFFFile class.
27
 * @see https://github.com/rochars/riff-file
28
 */
29
30
/** @module riff-file */
31
32
import {unpackString, unpack} from 'byte-data';
33
34
/**
35
 * A class to perform low-level reading of RIFF/RIFX files.
36
 */
37
export class RIFFFile {
38
39
  constructor() {
40
    /**
41
     * The container identifier.
42
     * 'RIFF', 'RIFX' and 'RF64' are supported.
43
     * @type {string}
44
     */
45
    this.container = '';
46
    /**
47
     * The main chunk size, in bytes.
48
     * @type {number}
49
     */
50
    this.chunkSize = 0;
51
    /**
52
     * The format identifier.
53
     * @type {string}
54
     */
55
    this.format = '';
56
    /**
57
     * An object representing the signature of all chunks in the file.
58
     * @type {{
59
      chunkId: string,
60
      chunkSize: number,
61
      format: string,
62
      chunkData: {start: number, end: number},
63
      subChunks: Array
64
      }|null}
65
     */
66
    this.signature = null;
67
    /**
68
     * @type {number}
69
     * @protected
70
     */
71
    this.head = 0;
72
    /**
73
     * @type {
74
      {bits: number, be: boolean, signed: boolean, fp: boolean}
75
     }
76
     * @protected
77
     */
78
    this.uInt32 = { bits: 32, be: false, signed: false, fp: false };
79
    /**
80
     * The list of supported containers.
81
     * Any format different from RIFX will be treated as RIFF.
82
     * @type {!Array<string>}
83
     * @protected
84
     */
85
    this.supported_containers = ['RIFF', 'RIFX'];
86
  }
87
88
  /**
89
   * Read the signature of the chunks in a RIFF/RIFX file.
90
   * @param {!Uint8Array} buffer The file bytes.
91
   * @protected
92
   */
93
  setSignature(buffer) {
94
      this.head = 0;
95
      this.container = this.readString(buffer, 4);
96
      if (this.supported_containers.indexOf(this.container) === -1) {
97
        throw Error('Not a supported format.');
98
      }
99
      this.uInt32.be = this.container === 'RIFX';
100
      this.chunkSize = this.readUInt32(buffer);
101
      this.format = this.readString(buffer, 4);
102
      // The RIFF file signature
103
      this.signature = {
104
          chunkId: this.container,
105
          chunkSize: this.chunkSize,
106
          format: this.format,
107
          subChunks: this.getSubChunksIndex_(buffer),
108
          chunkData: {start: 0, end: this.chunkSize}
109
      };
110
  }
111
112
  /**
113
    * Find a chunk by its fourCC_ in a array of RIFF chunks.
114
    * @param {string} chunkId The chunk fourCC_.
115
    * @param {boolean} multiple True if there may be multiple chunks
116
    *    with the same chunkId.
117
    * @return {Object}
118
    * @protected
119
    */
120
  findChunk(chunkId, multiple=false) {
121
    /** @type {!Array|null} */
122
    let chunks = this.signature.subChunks;
123
    /** @type {!Array<!Object>} */
124
    let chunk = [];
125
    for (let i=0; i<chunks.length; i++) {
126
      if (chunks[i].chunkId == chunkId) {
127
        if (multiple) {
128
          chunk.push(chunks[i]);
129
        } else {
130
          return chunks[i];
131
        }
132
      }
133
    }
134
    if (chunkId == 'LIST') {
135
      return chunk.length ? chunk : null;
136
    }
137
    return null;
138
  }
139
140
  /**
141
   * Read bytes as a string from a RIFF chunk.
142
   * @param {!Uint8Array} bytes The bytes.
143
   * @param {number} maxSize the max size of the string.
144
   * @return {string} The string.
145
   * @protected
146
   */
147
  readString(bytes, maxSize) {
148
    /** @type {string} */
149
    let str = '';
150
    str = unpackString(bytes, this.head, this.head + maxSize);
151
    this.head += maxSize;
152
    return str;
153
  }
154
155
  /**
156
   * Read a number from a chunk.
157
   * @param {!Uint8Array} bytes The chunk bytes.
158
   * @return {number} The number.
159
   * @protected
160
   */
161
  readUInt32(bytes) {
162
    /** @type {number} */
163
    let value = unpack(bytes, this.uInt32, this.head);
164
    this.head += 4;
165
    return value;
166
  }
167
168
  /**
169
   * Return the sub chunks of a RIFF file.
170
   * @param {!Uint8Array} buffer the RIFF file bytes.
171
   * @return {!Array<Object>} The subchunks of a RIFF/RIFX or LIST chunk.
172
   * @private
173
   */
174
  getSubChunksIndex_(buffer) {
175
      /** @type {!Array<!Object>} */
176
      let chunks = [];
177
      /** @type {number} */
178
      let i = this.head;
179
      while(i <= buffer.length - 8) {
180
          chunks.push(this.getSubChunkIndex_(buffer, i));
181
          i += 8 + chunks[chunks.length - 1].chunkSize;
182
          i = i % 2 ? i + 1 : i;
183
      }
184
      return chunks;
185
  }
186
187
  /**
188
   * Return a sub chunk from a RIFF file.
189
   * @param {!Uint8Array} buffer the RIFF file bytes.
190
   * @param {number} index The start index of the chunk.
191
   * @return {!Object} A subchunk of a RIFF/RIFX or LIST chunk.
192
   * @private
193
   */
194
  getSubChunkIndex_(buffer, index) {
195
      /** @type {!Object} */
196
      let chunk = {
197
          chunkId: this.getChunkId_(buffer, index),
198
          chunkSize: this.getChunkSize_(buffer, index),
199
      };
200
      if (chunk.chunkId == 'LIST') {
201
          chunk.format = unpackString(buffer, index + 8, index + 12);
202
          this.head += 4;
203
          chunk.subChunks = this.getSubChunksIndex_(buffer);
204
      } else {
205
          /** @type {number} */
206
          let realChunkSize = chunk.chunkSize % 2 ?
207
              chunk.chunkSize + 1 : chunk.chunkSize;
208
          this.head = index + 8 + realChunkSize;
209
          chunk.chunkData = {
210
              start: index + 8,
211
              end: this.head
212
          };
213
      }
214
      return chunk;
215
  }
216
217
  /**
218
   * Return the fourCC_ of a chunk.
219
   * @param {!Uint8Array} buffer the RIFF file bytes.
220
   * @param {number} index The start index of the chunk.
221
   * @return {string} The id of the chunk.
222
   * @private
223
   */
224
  getChunkId_(buffer, index) {
225
      this.head += 4;
226
      return unpackString(buffer, index, index + 4);
227
  }
228
229
  /**
230
   * Return the size of a chunk.
231
   * @param {!Uint8Array} buffer the RIFF file bytes.
232
   * @param {number} index The start index of the chunk.
233
   * @return {number} The size of the chunk without the id and size fields.
234
   * @private
235
   */
236
  getChunkSize_(buffer, index) {
237
      this.head += 4;
238
      return unpack(buffer, this.uInt32, index + 4);
239
  }
240
}
241